#include "music.h"
#include <QCoreApplication>
Music::Music()
    : isHistory(false)
    , isLike(false)
{

}

Music::Music(QUrl &url)
    : isHistory(false)
    , isLike(false)
    ,musicUrl(url)
{
    //读取url对应的歌曲文件的信息，解析出元数据
    //歌曲名称，歌曲作者，歌曲专辑，歌曲持续时长
    musicId=QUuid::createUuid().toString();
    parseMediaMetaData();
}
void Music::parseMediaMetaData()
{
    // 解析时候需要读取歌曲数据，读取歌曲⽂件需要⽤到QMediaPlayer类
    QMediaPlayer player;
    player.setMedia(musicUrl);
    // 媒体元数据解析需要时间，只有等待解析完成之后，才能提取⾳乐信息，此处循环等待
    // 循环等待时：主界⾯消息循环就⽆法处理了，因此需要在等待解析期间，让消息循环继续处理
    while(!player.isMetaDataAvailable())
    {
        QCoreApplication::processEvents();
    }
    // 解析媒体元数据结束，提取元数据信息
    if(player.isMetaDataAvailable())
    {
        //歌名
        musicName = player.metaData("Title").toString();
        //作者
        singerName = player.metaData("Author").toString();
        //专辑
        albumName = player.metaData("AlbumTitle").toString();
        duration = player.metaData("Duration").toLongLong();
        QString fileName =musicUrl.fileName();
        int index=fileName.indexOf('-');
        //歌名为空
        if(musicName.isEmpty())
        {
            if(index != -1)
            {
                musicName = fileName.mid(0, index).trimmed();
            }
            else
            {
                musicName = fileName.mid(0, fileName.indexOf('.')).trimmed();
            }
        }
        // 作者为空
        if(singerName.isEmpty())
        {
            if(index != -1)
            {
                singerName = fileName.mid(index+1, fileName.indexOf('.')-index-1).trimmed();
            }
            else
            {
                singerName = "未知歌⼿";
            }
        }
        //专辑为空
        if(albumName.isEmpty())
        {
            albumName = "专辑名未知";
        }
    }
}
void Music::setIsLike(bool isLike)
{
    this->isLike = isLike;
}
void Music::setIsHistory(bool isHistory)
{
    this->isHistory = isHistory;
}
void Music::setMusicName(const QString &musicName)
{
    this->musicName = musicName;
}
void Music::setSingerName(const QString &singerName)
{
    this->singerName = singerName;
}
void Music::setAlbumName(const QString &albumName)
{
    this->albumName = albumName;
}
void Music::setDuration(const qint64 duration)
{
    this->duration = duration;
}
void Music::setMusicUrl(const QUrl &url)
{
    this->musicUrl = url;
}
void Music::setMusicId(const QString &musicId)
{
    this->musicId = musicId;
}
bool Music::getIsLike()
{
    return isLike;
}
bool Music::getIsHistory()
{
    return isHistory;
}
QString Music::getMusicName() const
{
    return musicName;
}
QString Music::getSingerName() const
{
    return singerName;
}
QString Music::getAlbumName()
{
    return albumName;
}
qint64 Music::getDuration()
{
    return duration;
}
QUrl Music::getMusicUrl()
{
    return musicUrl;
}
QString Music::getMusicId()
{
    return musicId;
}

QString Music::getLrcFilepath() const
{
    QString lrcPath=musicUrl.toLocalFile();
    lrcPath.replace(".mp3",".lrc");
    lrcPath.replace(".flac",".lrc");
    lrcPath.replace(".mpga",".lrc");
    return lrcPath;
}

void Music::insertMusicToDB()
{
    QSqlQuery query;
    query.prepare("SELECT EXISTS (SELECT 1 FROM MusicInfo WHERE musicID =?)");
    query.addBindValue(musicId);
    if(!query.exec())
    {
        qDebug()<<"查询失败："<<query.lastError().text();
        return;
    }
    bool isExists=query.value(0).toBool();
    if(isExists)
    {
        //歌曲已经存在，不需要添加歌曲，只需要将isLike和isHistory属性更新
        query.prepare("UPDATE MusicInfo SET isLike = ?, isHistory = ? WHERE musicId = ?");
        query.addBindValue(isLike?1:0);
        query.addBindValue(isHistory?1:0);
        query.addBindValue(musicId);
        if(!query.exec())
        {
            qDebug()<<"更新失败"<<query.lastError().text();
        }
        qDebug()<<"更新music信息"<<musicName<<""<<musicId;
    }
    else
    {
        //歌曲不存在，则需要将歌曲信息插入数据库
        query.prepare("INSERT INTO MusicInfo(MusicId,musicName,SingerName,albumName,musicUrl,\
                                            duration,isLike,isHistory)\
                       VALUES(?,?,?,?,?,?,?,?)");
        query.addBindValue(musicId);
        query.addBindValue(musicName);
        query.addBindValue(singerName);
        query.addBindValue(albumName);
        query.addBindValue("file:///" + musicUrl.toLocalFile());
        query.addBindValue(duration);
        query.addBindValue(isLike ? 1 : 0);
        query.addBindValue(isHistory ? 1 : 0);
        if(query.exec())
        {
            qDebug()<<"更新失败："<<query.lastError().text();
        }
    }
}




























